package cn.com.bluemoon.daps.sync.core.sync.sql.mysql;

import cn.com.bluemoon.daps.sync.core.sync.sql.DdlSql;
import cn.com.bluemoon.daps.sync.core.sync.sql.SqlStrBuilder;
import lombok.Getter;
import lombok.Setter;

/**
 * <pre>
 *     column_definition: {
 *     data_type [NOT NULL | NULL] [DEFAULT default_value]
 *       [AUTO_INCREMENT] [UNIQUE [KEY]] [[PRIMARY] KEY]
 *       [COMMENT 'string']
 *       [COLLATE collation_name]
 *       [COLUMN_FORMAT {FIXED | DYNAMIC | DEFAULT}]
 *       [STORAGE {DISK | MEMORY}]
 *       [reference_definition]
 *   | data_type
 *       [COLLATE collation_name]
 *       [GENERATED ALWAYS] AS (expr)
 *       [VIRTUAL | STORED] [NOT NULL | NULL]
 *       [UNIQUE [KEY]] [[PRIMARY] KEY]
 *       [COMMENT 'string']
 *       [reference_definition]
 * }
 * </pre>
 *
 * @author Jarod.Kong
 * @date 2021/6/4 15:31
 */
@Setter
@Getter
public class MysqlColumnDefinition implements IKeyAddBlackQuote {
    private String columnName;
    private MySqlDataType dataType;
    private boolean zerofill;
    private boolean unSigned;
    private boolean notNull;
    private Object defaultValue;
    /**
     * autoIncrement=true下，自增初始值
     */
    private Long autoIncrementInitValue;
    private boolean autoIncrement;
    private boolean uniqueKey;
    private boolean primaryKey;
    private String comment;
    private String collationName;
    private Boolean addBlackQuote = true;

    @Override
    public String toString() {
        SqlStrBuilder sb = new SqlStrBuilder();
        if (addBlackQuote) {
            sb.appendBQ(columnName).appendWs();
        }else {
            sb.append(columnName).appendWs();
        }
        sb.append(getDataType().toString());
        if (unSigned) {
            sb.appendWS("unsigned");
        }
        if (zerofill){
            sb.appendWS("zerofill");
        }
        if (notNull) {
            sb.appendWS("not null");
        }
        if (autoIncrement) {
            // 如果为自增，则defaultValue必须为null，自增值不允许在修改该字段中进行default value，目前考虑用"alter table test_tb_1_1623052421972 auto_increment=1"
            // data_asset_platform> ALTER TABLE `test_tb_1_1623052421972` CHANGE COLUMN `id` `id` int(16) NOT NULL DEFAULT 1 AUTO_INCREMENT COMMENT '0' FIRST
            //[2021-06-11 09:36:40] [42000][1067] Invalid default value for 'id'
            sb.appendWS("auto_increment");
        }
        if (defaultValue != null) {
            Object dataTypeDefaultVal = getDataType().cast(defaultValue);
            if (!"".equals(dataTypeDefaultVal)) {
                sb.appendWS("default").append(dataTypeDefaultVal);
            }
        }
        if (uniqueKey) {
            sb.appendWS("unique key");
        }
        if (primaryKey) {
            sb.appendWS("primary key");
        }
        if (comment != null) {
            sb.appendWS("comment").appendSQ(DdlSql.formatComment0(comment)).appendWs();
        }
        if (collationName != null) {
            sb.appendWS("collate").append(collationName);
        }
        return sb.toString();
    }
}
